package com.example.zegodemo;


import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by liudong on 2018/3/22.
 */

public class LogUtil {
//    public static void e(String res) {
//        Logger.e(res);
////        writeToFile(res);
////        LogUtils.eTag("liudong", res);
//    }
//
//    public static void e(String title, String res) {
//        Logger.e(title + ":" + res);
////        writeToFile(res);
//    }

    public static void e_log(String res) {
        writeToFile(res);
    }

//    public static void w(String res) {
//        Logger.w(res);
//    }

    public static String logPath =BaseCoolApplication.getInstance().getExternalCacheDir().getPath() + "";

    //    public static String fileName = logPath + "/log_evowera_debug" + ".log";//log日志名
    public static String Time2fen(long l) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        return format.format(l);
    }

    private static final String logName = "kiki" + Time2fen(new Date().getTime());

    /**
     * 将log信息写入文件中
     */
    private static void writeToFile(String msg) {
        String log = Time2fen(new Date().getTime()) + ":" + msg + "\n";//log日志内容，可以自行定制

//        //如果父路径不存在
//        File file = new File(logPath);
//        if (!file.exists()) {
//            file.mkdirs();//创建父路径
//        }

        FileOutputStream fos = null;//FileOutputStream会自动调用底层的close()方法，不用关闭
        BufferedWriter bw = null;
        try {
            String name = logPath + "/" +
                    "kiki" + logName
                    + ".txt";
            fos = new FileOutputStream(name, true);//这里的第二个参数代表追加还是覆盖，true为追加，flase为覆盖
            bw = new BufferedWriter(new OutputStreamWriter(fos));
            bw.write(log);

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bw != null) {
                    bw.close();//关闭缓冲流
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}
